package me.leiusual.sharing.ui.store.dialog

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.StoreCategories
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.StoreRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class StoreCategoryViewModel @Inject constructor(
    private val storeRepository: StoreRepository,
    val store: Store
) : ViewModel() {

  // 分类0

  private val _storeCategories0 = MutableLiveData<ParamsStoreCategories>()
  val storeCategories0: LiveData<Data<StoreCategories.Response>> = switchMap(_storeCategories0) {
    storeRepository.storeCategories(it.parentCategoryId, it.storeId).toLiveData()
  }

  fun storeCategories0(parentCategoryId: Long?, storeId: Long) {
    _storeCategories0.value = ParamsStoreCategories(parentCategoryId, storeId)
  }

  // 分类1

  private val _storeCategories1 = MutableLiveData<ParamsStoreCategories>()
  val storeCategories1: LiveData<Data<StoreCategories.Response>> = switchMap(_storeCategories1) {
    storeRepository.storeCategories(it.parentCategoryId, it.storeId).toLiveData()
  }

  fun storeCategories1(parentCategoryId: Long?, storeId: Long) {
    _storeCategories1.value = ParamsStoreCategories(parentCategoryId, storeId)
  }

  // 分类2

  private val _storeCategories2 = MutableLiveData<ParamsStoreCategories>()
  val storeCategories2: LiveData<Data<StoreCategories.Response>> = switchMap(_storeCategories2) {
    storeRepository.storeCategories(it.parentCategoryId, it.storeId).toLiveData()
  }

  fun storeCategories2(parentCategoryId: Long?, storeId: Long) {
    _storeCategories2.value = ParamsStoreCategories(parentCategoryId, storeId)
  }

  data class ParamsStoreCategories(val parentCategoryId: Long?, val storeId: Long)
}